package warmachineroster.grapheditor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.ContextMenuProvider;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.gef.ui.actions.GEFActionConstants;
import org.eclipse.gef.ui.actions.UpdateAction;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.actions.ActionFactory;

import warmachineroster.RosterElement;
import warmachineroster.UnitRosterElement;
import warmachineroster.grapheditor.actions.AddUnitAttachmentAction;
import warmachineroster.grapheditor.actions.MinMaxUnitSizeAction;
import warmachineroster.grapheditor.dnd.WarmachineRosterCreationFactory;
import warmachinesystem.BaseUnit;
import warmachinesystem.Unit;
import warmachinesystem.UnitAttachment;

/**
 * 
 * 
 */
class WarmachineRosterEditorContextMenuProvider extends ContextMenuProvider {

	/** The editor's action registry. */
	private ActionRegistry actionRegistry;
	private IEditorPart editorPart;

	/**
	 * Instantiate a new menu context provider for the specified EditPartViewer
	 * and ActionRegistry.
	 * 
	 * @param viewer
	 *            the editor's graphical viewer
	 * @param registry
	 *            the editor's action registry
	 * @throws IllegalArgumentException
	 *             if registry is <tt>null</tt>.
	 */
	public WarmachineRosterEditorContextMenuProvider(EditPartViewer viewer,
			ActionRegistry registry, IEditorPart editorPart) {
		super(viewer);
		if (registry == null) {
			throw new IllegalArgumentException();
		}
		if (editorPart == null){
			throw new IllegalArgumentException();
		}
		this.editorPart = editorPart; 
		actionRegistry = registry;
	}

	/**
	 * Called when the context menu is about to show. Actions, whose state is
	 * enabled, will appear in the context menu.
	 * 
	 * @see org.eclipse.gef.ContextMenuProvider#buildContextMenu(org.eclipse.jface.action.IMenuManager)
	 */
	public void buildContextMenu(IMenuManager menu) {
		// Add standard action groups to the menu
		GEFActionConstants.addStandardActionGroups(menu);

		// Add actions to the menu
		menu.appendToGroup(GEFActionConstants.GROUP_UNDO, // target group id
				getAction(ActionFactory.UNDO.getId())); // action to add
		menu.appendToGroup(GEFActionConstants.GROUP_UNDO,
				getAction(ActionFactory.REDO.getId()));
		menu.appendToGroup(GEFActionConstants.GROUP_EDIT,
				getAction(ActionFactory.DELETE.getId()));
		menu.appendToGroup(GEFActionConstants.GROUP_EDIT,
				getAction(ActionFactory.SAVE.getId()));
		// faccio l'update della action in questo punto perchè deve essere
		// aggiornata l'etichetta ogni volta che il menù è visualizzato
		UpdateAction minmaxAction = (UpdateAction) getAction(MinMaxUnitSizeAction.MINMAX);
		minmaxAction.update();
		menu.appendToGroup(GEFActionConstants.GROUP_EDIT,
				getAction(MinMaxUnitSizeAction.MINMAX));
		
		EditPartViewer viewer = getViewer();
		EditPart targetEditPart = viewer.getFocusEditPart();
		Object model  = targetEditPart.getModel();
		if (model instanceof UnitRosterElement){
			Unit unit = (Unit) ((RosterElement) model).getUnitType();
			List<UnitAttachment> unitAttachments = unit.getArmyList().getUnitAttachments(unit);
			for (UnitAttachment ua : unitAttachments){
				CreateRequest req = new CreateRequest();
				req.setType(RequestConstants.REQ_CREATE);
				WarmachineRosterCreationFactory factory = new WarmachineRosterCreationFactory();
				List<BaseUnit>targetList = new ArrayList<BaseUnit>();
				targetList.add(ua);
				factory.setTransferredObjects(targetList);
				req.setFactory(factory);
//				req.setLocation(targetEditPart.);
				Command command = targetEditPart.getCommand(req);
				if (command.canExecute()){
					AddUnitAttachmentAction action = new AddUnitAttachmentAction(editorPart, command);
					action.setText("Attach " + ua.getName());
					menu.appendToGroup(GEFActionConstants.GROUP_EDIT,
							action);
				}
			}
		}
		
	}

	private IAction getAction(String actionId) {
		return actionRegistry.getAction(actionId);
	}

}
